package com.example.service;

import com.example.entity.Series;
import com.example.mapper.SeriesMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SeriesService {

    @Resource
    private SeriesMapper seriesMapper;

    public PageInfo<Series> selectPage(Series series, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Series> list = seriesMapper.selectAll(series);
        return PageInfo.of(list);
    }

    //获取用户推荐
    public List<Series> selectByUser(Integer id){
        return seriesMapper.selectByUserId(id);
    }

    //获取用户评论
    public List<Series> selectByComment(Integer id){
        return seriesMapper.selectByCommentId(id);
    }

    public Series selectById(Integer id){
        return seriesMapper.selectById(id);
    }

    public List<Series> selectRecommend(Integer id){
        List<Series> list = seriesMapper.selectAll(null);
        // 排除当前电影
        list = list.stream().filter(series -> !series.getId().equals(id)).collect(Collectors.toList());
        // 打乱顺序
        Collections.shuffle(list);
        return list;
    }

    public void add(Series series){
        seriesMapper.add(series);
    }

    public void update(Series series){
        seriesMapper.update(series);
    }

    public void delete(Integer id){
        seriesMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            seriesMapper.delete(id);
        }
    }

    public List<Series> getSeriesRecommend() {
        List<Series> seriesList = seriesMapper.sortTotal();
        return seriesList;
    }
}
